// 日期区间
laydate.render({
    elem: '#between'
    ,range: true
  });
// 车辆选择
$('.number').on('click',()=>{
    opsearch('{:url("html/car/car")}?op=search')
 })
// 添加维保记录跳转
$('#add').on('click',()=>{
    window.location.href = "{:url('html/plus/service_add')}"
})

var page = 1;
var pagenum = 10;
var fi=1;
var url={
    list:api.service.list,
    delete:api.service.delete
}
var data={};
data.page=page;
data.pagenum=pagenum;
var where_json ={};

{if $Think.get.filter}
       data.with_trashed=2;
       var main=[[ //表头   
       {field: 'id', title: '编号',  minWidth: 80, fixed: 'left'}
       ,{field: 'car', title: '车牌号',width: 140, templet: '#carId'}
       ,{field: 'start_date', title: '开始时间',width: 140}  
       ,{field: 'end_date', title: '结束时间',width: 140}
       ,{field:'service', title:'服务商',minWidth: 180,templet:'<div>{{d.service.service_name?d.service.service_name:"暂无信息"}}</div>'}
       ,{field:'address', title:'地点',minWidth: 180,templet:'<div>{{d.service.address?d.service.address:"暂无信息"}}</div>'}
       ,{field:'leader', title:'送维/送保人',width:140,templet:'<div>{{d.leader?d.leader:"无名氏"}}</div>'}
       ,{field:'service_type', title:'项目',templet:'#service_type',width:100}
       ,{field:'ps', title:'备注',templet:'<div>{{d.ps?d.ps:"无备注信息"}}</div>',minWidth:180}
    ]];
{else/}
var main=[[ //表头
    {type:'checkbox', fixed: 'left'}
   ,{field: 'id', title: '编号',  minWidth: 80, fixed: 'left'}
   ,{field: 'car', title: '车牌号',width: 140, templet: '#carId'}
   ,{field: 'start_date', title: '开始时间',width: 140}  
   ,{field: 'end_date', title: '结束时间',width: 140}
   ,{field:'service', title:'服务商',minWidth: 180,templet:'<div>{{d.service.service_name?d.service.service_name:"暂无信息"}}</div>'}
   ,{field:'address', title:'地点',minWidth: 180,templet:'<div>{{d.service.address?d.service.address:"暂无信息"}}</div>'}
   ,{field:'leader', title:'送维/送保人',width:140,templet:'<div>{{d.leader?d.leader:"无名氏"}}</div>'}
   ,{field:'service_type', title:'项目',templet:'#service_type',width:100}
   ,{field:'ps', title:'备注',templet:'<div>{{d.ps?d.ps:"无备注信息"}}</div>',minWidth:180}
   ,{ title: '操作',width:180, toolbar:'#demo',fixed: 'right',align:'center'}
]];
{/if}

{if $Think.get.car_id}
where_json['car.car_id']=['in','{$Think.get.car_id}']
{/if}
{if $Think.get.between}
        var start = '{$Think.get.between}'.split(' ');
		where_json.start_date=['between time',[start[0],start[2]]]
{/if}


data.where_json=JSON.stringify(where_json)

ajaxsevice(data);
function ajaxsevice(data) {
    Load();
    //请求数据
    ajax(url.list, data, 'post').done(function (resp) {
        Load(1);
        // 成功回调
        //方法级表单渲染
        console.log(resp)
       
        table.render({
            elem: '#service',
            cols: main,
            data: resp.data,
            limit: pagenum,
            cellMinWidth: 80
        });
        if (fi == 1) {
            laypage.render({
                elem: 'page',
                count: resp.count,
                layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
                theme: '#1E9FFF',
                jump: function (obj, first) {
                    console.log(obj)
                    if (!first) {
                        page = obj.curr;
                        data.page = page;
                        pagenum = obj.limit;
                        data.pagenum = pagenum;
                        ajaxsevice(data);
                    } else {

                    }
                }
            })
            fi = 2;
        }
        // 操作
        table.on('tool(demo)', function (obj) {
                    var data1 = obj.data; //获得当前行数据
                    var data2 = {
                        ids:data1.id
                    };
                    var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                    console.log('caizuo')
                    console.log(data2)
                    if (layEvent === 'edit') {
                        window.location.href = "{:url('/html/plus/service_add')}?id="+data1.id;
                    } else if (layEvent === 'del') {
                        layer.confirm('是否删除', function (index) {
                                Load()
                                ajax(url.delete, data2, 'post').done(function (res) {
                                    // 成功回调
                                    console.log(res)
                                    layer.closeAll();
                                    Msg('删除成功');
                                    ajaxsevice(data)
                                }).fail(function (err) {
                                    // 失败回调
                                    layer.close(deleteload);
                                    Err(res.msg ? res.msg : '操作失败，稍候再试');
                                });
                                layer.close(index);
                            })
                        }else if(layEvent == 'approve'){
                            window.location.href = "{:url('/html/plus/service_approve')}?id="+data1.id;
                        }
                    })
            			//删除多个
    			$(".delete-multiple").on('click', function () {
    				var ids = '';
                    var checkStatus = table.checkStatus('service'); //test即为基础参数id对应的值
                    console.log('多选信息')
                    console.log(checkStatus)
    				console.log(checkStatus.data) //获取选中行的数据
    				console.log(checkStatus.data.length) //获取选中行数量，可作为是否有选中行的条件
                    console.log(checkStatus.isAll) //表格是否全选
                    if(checkStatus.data.length){
    				for (var dei = 0; dei < checkStatus.data.length; dei++) {
    					if (dei == 0) {
    						ids = checkStatus.data[dei].id
    					} else {
    						ids += ("," + checkStatus.data[dei].id)
    					}
                    }
                    console.log('删除')
                    console.log(ids)
                    var data2 ={
                        ids : ids
                    }
    				layer.open({
    					type: 1,
    					title: false,
    					id: "delete",
    					content: '<div style="padding: 20px 100px;">是否删除</div>',
    					btn: ['确定', '取消'],
    					btnAlign: 'c' //按钮居中
    						,
    					shade: 0 //不显示遮罩
    						,
    					yes: function () {
    						var deleteload = layer.load(1, {
    							shade: [0.1, '#fff'] //0.1透明度的白色背景
                            });
                            console.log(data2)
                            ajax(url.delete,data2,'post').done(function(resp) {
                                // 成功回调
                            			layer.close(deleteload);
                                        layer.closeAll();
                                        Msg('删除成功');
                                        ajaxsevice(data)
                                }).fail(function(err) {
                                // 失败回调
                                layer.close(deleteload);
    						    Err(res.msg ? res.msg : '操作失败，稍候再试');
                                });
    					},
    					btn2: function () {
    						layer.closeAll();
    					}
                    });
                }else{
                    Err('未有选择项')
                }
                });
    if(resp.code == 1){
    }else{
        Err(resp.msg)
    }
    }).fail(function (err) {
        // 失败回调
        Load(1)
        Err('请求失败')
    });
};

